{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sklearn中的Scaler使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn import datasets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "iris = datasets.load_iris()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X = iris.data\n",
    "y = iris.target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 5.1,  3.5,  1.4,  0.2],\n",
       "       [ 4.9,  3. ,  1.4,  0.2],\n",
       "       [ 4.7,  3.2,  1.3,  0.2],\n",
       "       [ 4.6,  3.1,  1.5,  0.2],\n",
       "       [ 5. ,  3.6,  1.4,  0.2],\n",
       "       [ 5.4,  3.9,  1.7,  0.4],\n",
       "       [ 4.6,  3.4,  1.4,  0.3],\n",
       "       [ 5. ,  3.4,  1.5,  0.2],\n",
       "       [ 4.4,  2.9,  1.4,  0.2],\n",
       "       [ 4.9,  3.1,  1.5,  0.1]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[:10, :] # 前10行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# StandardScaler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import StandardScaler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    " standardScaler = StandardScaler()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "StandardScaler(copy=True, with_mean=True, with_std=True)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "standardScaler.fit(X_train)  # 归一化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 5.84910714,  3.04017857,  3.83482143,  1.23125   ])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "standardScaler.mean_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/liangshanguang/anaconda/lib/python3.6/site-packages/sklearn/utils/deprecation.py:70: DeprecationWarning: Function std_ is deprecated; Attribute ``std_`` will be removed in 0.19. Use ``scale_`` instead\n",
      "  warnings.warn(msg, category=DeprecationWarning)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([ 0.84915878,  0.41650326,  1.76845927,  0.77171322])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "standardScaler.std_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.84915878,  0.41650326,  1.76845927,  0.77171322])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "standardScaler.scale_ # std建议用scale替换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.59086014,  1.34409854,  1.28087687,  1.64406928],\n",
       "       [ 0.41322408,  0.86391023,  0.88505209,  1.38490565],\n",
       "       [-1.1177028 , -0.0964664 , -1.37680379, -1.33631247],\n",
       "       [-0.76441198,  0.86391023, -1.37680379, -1.33631247],\n",
       "       [ 1.00204211,  0.14362775,  0.31958812,  0.21866931],\n",
       "       [ 0.53098769,  0.86391023,  0.99814488,  1.51448747],\n",
       "       [ 1.00204211,  0.14362775,  0.48922731,  0.34825113],\n",
       "       [-0.05783034, -1.05684303,  0.09340253, -0.04049432],\n",
       "       [-0.52888477,  2.06438101, -1.2071646 , -1.07714884],\n",
       "       [-0.88217559, -1.29693719, -0.47206144, -0.17007613],\n",
       "       [ 0.29546048, -0.0964664 ,  0.43268091,  0.21866931],\n",
       "       [-1.1177028 , -1.53703135, -0.30242225, -0.29965795],\n",
       "       [-0.99993919,  1.10400438, -1.43335019, -1.20673065],\n",
       "       [-0.64664837,  1.5841927 , -1.3202574 , -1.33631247],\n",
       "       [ 0.7665149 , -0.0964664 ,  1.11123768,  1.25532384],\n",
       "       [ 0.53098769, -0.33656056,  0.99814488,  0.73699657],\n",
       "       [ 1.23756933,  0.14362775,  0.6023201 ,  0.34825113],\n",
       "       [ 1.11980572, -0.57665472,  0.54577371,  0.21866931],\n",
       "       [ 0.64875129,  0.14362775,  0.94159849,  0.73699657],\n",
       "       [ 2.17967818, -0.0964664 ,  1.28087687,  1.38490565],\n",
       "       [ 1.00204211, -0.0964664 ,  0.6588665 ,  0.60741476],\n",
       "       [ 1.23756933,  0.14362775,  0.7154129 ,  1.38490565],\n",
       "       [-0.05783034, -0.81674887,  0.7154129 ,  0.86657839],\n",
       "       [ 0.53098769, -0.57665472,  0.7154129 ,  0.34825113],\n",
       "       [-0.88217559,  1.82428686, -1.09407181, -1.07714884],\n",
       "       [-1.47099362,  1.34409854, -1.60298938, -1.33631247],\n",
       "       [-1.47099362,  0.86391023, -1.37680379, -1.20673065],\n",
       "       [ 0.17769687, -0.0964664 ,  0.54577371,  0.73699657],\n",
       "       [ 0.05993326,  0.38372191,  0.54577371,  0.73699657],\n",
       "       [ 0.53098769,  0.62381607,  1.22433047,  1.64406928],\n",
       "       [-1.35323001,  0.38372191, -1.263711  , -1.33631247],\n",
       "       [ 0.64875129, -0.57665472,  0.99814488,  1.12574202],\n",
       "       [ 0.88427851, -0.0964664 ,  0.31958812,  0.21866931],\n",
       "       [-0.41112116, -1.53703135, -0.07623666, -0.29965795],\n",
       "       [-0.76441198,  1.10400438, -1.3202574 , -1.33631247],\n",
       "       [-0.41112116, -1.05684303,  0.31958812, -0.04049432],\n",
       "       [ 2.06191457, -0.0964664 ,  1.56360886,  1.12574202],\n",
       "       [-0.17559395, -0.0964664 ,  0.20649533, -0.04049432],\n",
       "       [-1.1177028 ,  0.14362775, -1.3202574 , -1.46589429],\n",
       "       [-0.05783034, -0.81674887,  0.03685613, -0.04049432],\n",
       "       [-0.99993919, -0.0964664 , -1.263711  , -1.33631247],\n",
       "       [ 1.23756933,  0.14362775,  0.88505209,  1.12574202],\n",
       "       [ 0.53098769, -1.7771255 ,  0.31958812,  0.0890875 ],\n",
       "       [-1.2354664 ,  0.14362775, -1.263711  , -1.33631247],\n",
       "       [ 0.53098769, -0.81674887,  0.6023201 ,  0.73699657],\n",
       "       [-1.70652083,  0.38372191, -1.43335019, -1.33631247],\n",
       "       [-0.76441198,  2.54456933, -1.3202574 , -1.46589429],\n",
       "       [-1.2354664 , -0.0964664 , -1.37680379, -1.46589429],\n",
       "       [ 0.41322408, -2.01721966,  0.37613452,  0.34825113],\n",
       "       [ 0.29546048, -0.57665472,  0.48922731, -0.04049432],\n",
       "       [ 0.7665149 ,  0.38372191,  0.7154129 ,  0.99616021],\n",
       "       [-0.17559395, -0.33656056,  0.20649533,  0.0890875 ],\n",
       "       [-0.17559395,  3.2648518 , -1.3202574 , -1.07714884],\n",
       "       [ 0.7665149 , -0.0964664 ,  0.7719593 ,  0.99616021],\n",
       "       [-0.05783034, -0.57665472,  0.7154129 ,  1.51448747],\n",
       "       [ 1.70862375, -0.33656056,  1.39396966,  0.73699657],\n",
       "       [-1.58875722, -1.7771255 , -1.43335019, -1.20673065],\n",
       "       [-0.17559395,  1.82428686, -1.2071646 , -1.20673065],\n",
       "       [ 0.41322408, -0.57665472,  0.54577371,  0.73699657],\n",
       "       [ 1.59086014, -0.0964664 ,  1.11123768,  0.47783294],\n",
       "       [-0.88217559,  1.82428686, -1.263711  , -1.33631247],\n",
       "       [-1.2354664 , -0.0964664 , -1.37680379, -1.20673065],\n",
       "       [ 0.17769687, -0.33656056,  0.37613452,  0.34825113],\n",
       "       [-0.41112116,  1.10400438, -1.43335019, -1.33631247],\n",
       "       [-0.41112116, -1.7771255 ,  0.09340253,  0.0890875 ],\n",
       "       [ 0.64875129, -0.57665472,  0.99814488,  1.25532384],\n",
       "       [-0.88217559,  1.10400438, -1.37680379, -1.20673065],\n",
       "       [ 0.29546048, -1.05684303,  0.99814488,  0.21866931],\n",
       "       [ 0.29546048, -0.33656056,  0.48922731,  0.21866931],\n",
       "       [-1.82428444, -0.0964664 , -1.54644298, -1.46589429],\n",
       "       [ 2.17967818,  1.82428686,  1.62015525,  1.25532384],\n",
       "       [ 1.11980572,  0.38372191,  1.16778408,  1.38490565],\n",
       "       [-0.05783034, -0.81674887,  0.7154129 ,  0.86657839],\n",
       "       [-1.2354664 ,  0.86391023, -1.09407181, -1.33631247],\n",
       "       [ 0.17769687, -2.01721966,  0.09340253, -0.29965795],\n",
       "       [ 0.41322408, -0.33656056,  0.26304172,  0.0890875 ],\n",
       "       [-0.29335755, -0.0964664 ,  0.37613452,  0.34825113],\n",
       "       [-1.47099362,  0.38372191, -1.37680379, -1.33631247],\n",
       "       [ 1.82638736, -0.57665472,  1.28087687,  0.86657839],\n",
       "       [ 1.00204211, -0.0964664 ,  0.7719593 ,  1.38490565],\n",
       "       [-0.76441198, -0.81674887,  0.03685613,  0.21866931],\n",
       "       [ 1.00204211,  0.14362775,  0.99814488,  1.51448747],\n",
       "       [-0.99993919,  0.62381607, -1.37680379, -1.33631247],\n",
       "       [-1.1177028 ,  0.14362775, -1.3202574 , -1.46589429],\n",
       "       [-1.47099362,  0.14362775, -1.3202574 , -1.33631247],\n",
       "       [ 2.17967818, -1.05684303,  1.73324805,  1.38490565],\n",
       "       [ 0.7665149 , -0.0964664 ,  0.94159849,  0.73699657],\n",
       "       [-0.99993919,  0.38372191, -1.48989659, -1.33631247],\n",
       "       [-0.99993919, -1.7771255 , -0.30242225, -0.29965795],\n",
       "       [ 0.29546048, -0.0964664 ,  0.6023201 ,  0.73699657],\n",
       "       [-0.99993919,  0.86391023, -1.263711  , -1.07714884],\n",
       "       [-0.88217559,  1.82428686, -1.3202574 , -1.20673065],\n",
       "       [ 0.17769687, -0.81674887,  0.7154129 ,  0.47783294],\n",
       "       [-0.17559395, -0.57665472,  0.14994893,  0.0890875 ],\n",
       "       [ 0.53098769, -1.29693719,  0.6023201 ,  0.34825113],\n",
       "       [ 1.00204211,  0.62381607,  1.05469128,  1.64406928],\n",
       "       [-1.1177028 , -1.29693719,  0.37613452,  0.60741476],\n",
       "       [ 0.17769687, -2.01721966,  0.6588665 ,  0.34825113],\n",
       "       [-0.88217559,  0.86391023, -1.3202574 , -1.33631247],\n",
       "       [ 1.59086014,  0.38372191,  1.22433047,  0.73699657],\n",
       "       [-1.70652083, -0.33656056, -1.37680379, -1.33631247],\n",
       "       [ 0.17769687,  0.86391023,  0.37613452,  0.47783294],\n",
       "       [-0.52888477, -0.0964664 ,  0.37613452,  0.34825113],\n",
       "       [ 1.35533293,  0.38372191,  0.48922731,  0.21866931],\n",
       "       [-0.88217559,  1.5841927 , -1.3202574 , -1.07714884],\n",
       "       [-0.29335755, -0.33656056, -0.13278306,  0.0890875 ],\n",
       "       [-1.2354664 ,  0.86391023, -1.263711  , -1.33631247],\n",
       "       [-0.17559395, -1.29693719,  0.6588665 ,  0.99616021],\n",
       "       [-0.29335755, -0.57665472,  0.6023201 ,  0.99616021],\n",
       "       [ 0.53098769,  0.62381607,  0.48922731,  0.47783294],\n",
       "       [-0.17559395, -1.05684303, -0.18932945, -0.29965795],\n",
       "       [ 2.17967818, -0.57665472,  1.62015525,  0.99616021]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "standardScaler.transform(X_train) # 对整个矩阵进行归一化，不会改变X_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X_train = standardScaler.transform(X_train) # 对整个矩阵进行归一化，不会改变X_train,所以需要赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X_test_standard = standardScaler.transform(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "knn_clf = KNeighborsClassifier(n_neighbors=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
       "           metric_params=None, n_jobs=1, n_neighbors=3, p=2,\n",
       "           weights='uniform')"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn_clf.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.97368421052631582"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn_clf.score(X_test_standard, y_test) # 获取计算地准确率。训练数据集和测试数据集必须都进行归一化(transform)操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
